package com.cs.store.controller;

import com.cs.common.result.HttpResp;
import com.cs.common.result.ResultCode;
import com.cs.domain.vo.OrderVo;
import com.cs.store.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/userOrder")
public class UserOrderController {
    @Autowired
    private IOrderService iOrderService;


    /**
     * 展示用户订单
     * @param username
     * @param response
     * @return
     */
    @GetMapping("findUserOrder")
    public HttpResp findUserOrder(String username, HttpServletResponse response) {
        List<OrderVo> list =iOrderService.findUserOrder(username);
        List<OrderVo> after = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(OrderVo::getOrderNum))), ArrayList::new));
        return HttpResp.result(ResultCode.USER_ORDER_SHOW, new Date(),after );
    }
    @GetMapping("findStoreOrder")
    public HttpResp findStoreOrder(String storeName, HttpServletResponse response) {
        List<OrderVo> list =iOrderService.findStoreOrder(storeName);
        List<OrderVo> after = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(OrderVo::getOrderNum))), ArrayList::new));
        return HttpResp.result(ResultCode.USER_ORDER_SHOW, new Date(),after );
    }
    @GetMapping("oneUserOrder")
    public HttpResp oneUserOrder(String orderNum, HttpServletResponse response) {
        List<OrderVo> list =iOrderService.oneUserOrder(orderNum);
        return HttpResp.result(ResultCode.USER_ORDER_SHOW, new Date(),list);
    }


    @GetMapping("delUserOrder")
    public HttpResp delUserOrder(Long orderId, HttpServletResponse response) {

        return HttpResp.result(ResultCode.USER_ORDER_DEL, new Date(), iOrderService.delUserOrder(orderId));

    }




}
